package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.rcloud.io.Utils.EndorsementVo;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.Article;
import org.rcloud.io.model.Endorsement;
import org.rcloud.io.model.File;
import org.rcloud.io.service.ArticleService;
import org.rcloud.io.service.EndorsementService;
import org.rcloud.io.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/2/15.
 */
@RestController
@RequestMapping(value = "/file")
public class EndorsementController {

    @Autowired
    private EndorsementService endorsementService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private FileService fileService;

     /*
    * 保存代言家庭信息
    * */
    @ApiOperation(value="保存代言家庭", notes="根据Endorsement对象保存代言家庭")
    @ResponseBody
    @RequestMapping(value = "/saveEndorsementinfo", produces = {"application/json;charset=UTF-8"})
    public boolean saveEndorsement(@RequestBody String params){

        JSONObject object =JSON.parseObject(params);

        Map map = JSON.parseObject(object.toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String video = map.get("video").toString();
        String thumbnail = map.get("thumbnail").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        String url = video.substring(video.lastIndexOf("/")+1);
        String vid =url.substring(0,url.indexOf("."));
        System.out.println(url);
        System.out.println(vid);

        String nameTitle = name+System.currentTimeMillis();//生成时间戳,确保两张表能够关联

        Endorsement endorsement = new Endorsement();
        endorsement.setEid(eid);
        endorsement.setName(name);
        endorsement.setAddress(address);
        endorsement.setLongitude(longitude);
        endorsement.setLatitude(latitude);
        endorsement.setEtype(etype);
        endorsement.setSummary(summary);
        endorsement.setVideo(video);
        endorsement.setThumbnail(thumbnail);
        endorsement.setReport(report);
        endorsement.setUsername(username);
        endorsement.setUserphone(userphone);
        endorsement.setNametitle(nameTitle);
        endorsement.setInf01(vid);

        Article article = new Article();
        article.setTitle(nameTitle);

        //保存代言家庭
        int end = Integer.valueOf(endorsementService.save(endorsement).getInfo().toString());

//        System.out.println("将代言家庭的数据保存,状态为:"+end);
        if(end!=1){
            return false;
        }

        //保存文章
        int art = Integer.valueOf(articleService.save(article).getInfo().toString());
//        System.out.println("将文章的数据保存,状态为:"+art);
        if(art!=1){
            return false;
        }

        String shouming = articleService.findTitle(nameTitle).getInfo().toString();
//        System.out.println("查找文章的id为="+shouming);

        int upd = Integer.valueOf(endorsementService.updateArtid(nameTitle,shouming).getInfo().toString());
//        System.out.println("修改刚刚保存的代言家庭信息,就是将文章id放代言家庭到表中,返回的状态为="+upd);
        if (upd!=1){
            return false;
        }



       return true;
    }


     /*
    * 获取所有的代言家庭信息
    * */
    @ApiOperation(value="获取代言家庭", notes="根据获取代言家庭")
    @ResponseBody
    @RequestMapping(value = "/findEndorsementinfo", produces = {"application/json;charset=UTF-8"})
    public PageVo<Endorsement> findEndorsement(){
        //获取代言家庭信息
        PageVo<Endorsement> endorsement1 = endorsementService.findEndorsement();
        if (endorsement1.getCode() == 0) {
            List<Endorsement> endorsement = endorsementService.findEndorsement().getInfo();


            //获取图片信息
            List<File> file = fileService.findFile().getInfo();

            for (int i =0;i<endorsement.size();i++){
                String did = endorsement.get(i).getEid();
                List temp = new ArrayList();
                for (int j=0;j<file.size();j++) {
                    // System.out.println(file.get(i));
                    String fid = file.get(j).getEid();
                    if (fid.equals(did)) {
                        temp.add(file.get(j));
                    }
                }
                endorsement.get(i).setRemark(JSON.toJSONString(temp));
            }

            return PageVo.setOk(endorsement);

        } else {
            return endorsement1;
        }

    }


    /*
    * 根据Num获取代言家庭信息
    * */
    @ApiOperation(value="获取代言家庭", notes="根据Num获取代言家庭信息")
    @ResponseBody
    @RequestMapping(value = "/findEndorsementinfoByNum", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo findEndorsementByNum(String eid){
        return endorsementService.findEndorsementByNum(eid);
    }

    /*
   * 根据id获取代言家庭信息
   * */
    @ApiOperation(value="获取代言家庭", notes="根据id获取代言家庭信息")
    @ResponseBody
    @RequestMapping(value = "/findEndorsementinfoById", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo findEndorsementById(@RequestBody String params){
        String[] ids = params.split("=");
        int id=Integer.parseInt(ids[1]);
        return endorsementService.findEndorsementById(id);
    }

        /*
        * 根据id修改代言家庭信息
        * */
    @ApiOperation(value="修改代言家庭", notes="根据id修改代言家庭信息")
    @ResponseBody
    @RequestMapping(value = "/updateEndorsementinfoById", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo UpdateEndorsementById(@RequestBody String params){

        JSONObject object = JSON.parseObject(params);

        Map map = JSON.parseObject(object.toString());
        int id = Integer.parseInt(map.get("id").toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String video = map.get("video").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        Endorsement endorsement = new Endorsement();
        endorsement.setId(id);
        endorsement.setEid(eid);
        endorsement.setName(name);
        endorsement.setAddress(address);
        endorsement.setLongitude(longitude);
        endorsement.setLatitude(latitude);
        endorsement.setEtype(etype);
        endorsement.setSummary(summary);
        endorsement.setVideo(video);
        endorsement.setReport(report);
        endorsement.setUsername(username);
        endorsement.setUserphone(userphone);

        return endorsementService.updateById(endorsement);
    }

    /*
   * 分页查询（带参数）
   *
   * */
    @RequestMapping(value = "/selectByOther", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<Endorsement> selectByOther(@RequestBody String params) {
        JSONObject obj = JSON.parseObject(params);
        Map map = JSON.parseObject(obj.get("params").toString());
        int pageNum = Integer.parseInt(obj.get("pageNum").toString());
        int pageSize = Integer.parseInt(obj.get("pageSize").toString());

        PageInfo<Endorsement> PageInfo = new PageInfo<Endorsement>();
        if(pageNum == 1 || pageNum == 2){
            PageInfo = endorsementService.selectByOther(pageNum, pageSize, map);
        }else{
            PageInfo = endorsementService.selectByOthers(pageNum-2, pageSize, map);
        }


        //获取图片信息
        List<File> file =fileService.findFile().getInfo();

        List<Endorsement> endorsement = PageInfo.getList();

        for (int i =0;i<endorsement.size();i++){
            String did = endorsement.get(i).getEid();
            List temp = new ArrayList();
            for (int j=0;j<file.size();j++) {
                String fid = file.get(j).getEid();
                if (fid.equals(did)) {
                    temp.add(file.get(j));
                }
            }
            endorsement.get(i).setRemark(JSON.toJSONString(temp));
        }
        PageInfo.setList(endorsement);

        return PageInfo;

    }

    /**
     *  4个乡镇返回2个代言家庭
     * */

    @RequestMapping(value = "/selectByTwo", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageVo<Endorsement> findEndorsementByTwo(){
        List<Endorsement> dq = endorsementService.findEndorsementByDQ().getInfo();
        List<Endorsement> ws = endorsementService.findEndorsementByWS().getInfo();
        List<Endorsement> xd = endorsementService.findEndorsementByXD().getInfo();
        List<Endorsement> xk = endorsementService.findEndorsementByXK().getInfo();

        List<Endorsement> endorsement = new ArrayList<Endorsement>();
        endorsement.addAll(dq);
        endorsement.addAll(ws);
        endorsement.addAll(xd);
        endorsement.addAll(xk);

        //获取图片信息
        List<File> file =fileService.findFile().getInfo();

        for (int i =0;i<endorsement.size();i++){
            String did = endorsement.get(i).getEid();
            List temp = new ArrayList();
            for (int j=0;j<file.size();j++) {
                String fid = file.get(j).getEid();
                if (fid.equals(did)) {
                    temp.add(file.get(j));
                }
            }

            endorsement.get(i).setRemark(JSON.toJSONString(temp));
           /* System.out.println("+++++"+JSON.toJSONString(temp)+"+++++++");*/
        }

        return PageVo.setOk(endorsement);
    }

    /**
     *  根据类型查询代言家庭
     * */
    @RequestMapping(value = "/selectByEtype", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageVo<Endorsement> findEndorsementByEtype(@RequestBody String params){

        JSONObject object= JSON.parseObject(params);

        String name =  object.getString("name");
        String etype =  object.getString("etype");
//        System.out.println(etype+"-++++++++++");
        List<Endorsement> endorsement = new ArrayList<Endorsement>();

        if("".equals(name)){
            endorsement = endorsementService.findEndorsementByEtype(etype).getInfo();
        }else{
            endorsement = endorsementService.findEndorsementByName(name).getInfo();
        }

        //获取图片信息
        List<File> file =fileService.findFile().getInfo();

        for (int i =0;i<endorsement.size();i++){
            String did = endorsement.get(i).getEid();
//            System.out.println(i);
            List temp = new ArrayList();
            for (int j=0;j<file.size();j++) {
                // System.out.println(file.get(i));
                String fid = file.get(j).getEid();
                if (fid.equals(did)) {
//                    System.out.println(did + "...."+fid);

                    temp.add(file.get(j));
                }
            }

            endorsement.get(i).setRemark(JSON.toJSONString(temp));
        }

        return PageVo.setOk(endorsement);

    }

    /**
     *  根据搜索内容查询代言家庭
     * */
    @RequestMapping(value = "/findEndorsementByValue", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageVo<Endorsement> findEndorsementByValue(@RequestBody String params) {

        List<Endorsement> endorsement = endorsementService.findEndorsementByValue(params).getInfo();

        //获取图片信息
        List<File> file =fileService.findFile().getInfo();

        for (int i =0;i<endorsement.size();i++){
            String did = endorsement.get(i).getEid();
            List temp = new ArrayList();
            for (int j=0;j<file.size();j++) {
                String fid = file.get(j).getEid();
                if (fid.equals(did)) {

                    temp.add(file.get(j));
                }
            }

            endorsement.get(i).setRemark(JSON.toJSONString(temp));
        }

        return PageVo.setOk(endorsement);
    }

    @RequestMapping(value = "/findEndorsementByCondition", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<EndorsementVo> findByCondition(int pagenum, int pagesize, Endorsement endorsement) {
        return endorsementService.findByCondition(pagenum,pagesize,endorsement);
    }

    @RequestMapping("/random")
    @ResponseBody
    @CrossOrigin
    public ResultVo random(){
        return endorsementService.randomShow();
    }

}
